<script setup>
import { ref,onMounted } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { useRouter } from "vue-router";
import { getRechargeListAPI,rechargeAPI } from "@/apis/recharge";
const router = useRouter();
const rechargeList = ref([])
const pageInfo = ref({
  current: 1,
  size: 8,
  total: 0
})
const form = ref({
  userId: '',
  balance: ''
})

const getRechargeList = async (size=10,current=1) => {
  const res = await getRechargeListAPI(size,current);
  console.log(res);
  rechargeList.value = res.data.records;
  pageInfo.value.total = parseInt(res.data.total, 10);
  pageInfo.value.size = res.data.size;
  pageInfo.value.current = res.data.current;
}

const handlePageChange = (current) => {
  pageInfo.value.current = current;
  getRechargeList(10,current);
}

const handleAdd = async () => {
  const res = await rechargeAPI(form.value);
  console.log(res);
  if(res.status === 200){
    ElMessage.success(res.message);
    getRechargeList();
  }else{
    ElMessage.error(res.message);
  }
}


onMounted(() => {
  getRechargeList();
})
</script>

<template>
  <el-row >
    <el-form :inline="true" :model="form">
      <el-form-item label="用户ID">
        <el-input v-model="form.userId" placeholder="请输入用户ID" />
      </el-form-item>
      <el-form-item label="金额">
        <el-input v-model="form.balance" placeholder="请输入金额" />
      </el-form-item>
      <el-form-item>
      <el-button :icon="Plus" @click="handleAdd">充值</el-button>
      </el-form-item>
    </el-form>
    
  </el-row>
  <el-row>
    <el-table :data="rechargeList" style="width: 100%">
      <el-table-column prop="id" label="ID"  />
      <el-table-column prop="createTime" label="创建时间"  /> 
      <el-table-column prop="userId" label="用户ID"  /> 
      <el-table-column prop="balance" label="金额"  /> 
    </el-table>
    <div class="product-list-pagination">
    <el-pagination v-model:currentPage="pageInfo.current" :page-size="pageInfo.size" :total="pageInfo.total"
      layout="total, prev, pager, next" @current-change="handlePageChange"></el-pagination>
  </div>
  </el-row>

</template>

<style lang="scss" scoped>
.product-list-pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>